<?php

namespace builder;

/**
 * 建造者模式的具体角色和职责
 * 1. 产品类（Product）：要构建的复杂对象，包含多个组成部分
 * 2. 抽象建造者类（Builder）：定义构建产品各个部分的抽象方法，以及返回最终产品的方法
 * 3. 具体建造者类（ConcreteBuilder）：实现Builder接口，具体负责各个部分的构建细节，并最终组装出完成产品
 * 4. 指挥者类（Director）：统一建造者按照一定的步骤来构建产品，屏蔽构建过程中的细节
 * 5. 客户端调用
 */

// 以构建游戏电脑和办公电脑为例
// 1. 产品类（Product）
class Computer
{
    private string $cpu;
    private string $gpu;
    private string $memory;
    private string $hardDisk;

    public function setCpu(string $cpu): void
    {
       $this->cpu = $cpu;
    }
    public function setGpu(string $gpu): void
    {
       $this->gpu = $gpu;
    }
    public function setMemory(string $memory): void
    {
       $this->memory = $memory;
    }
    public function setHardDisk(string $hardDisk): void
    {
       $this->hardDisk = $hardDisk;
    }
    public function show(): void
    {
        echo "CPU: {$this->cpu}\n";
        echo "GPU: {$this->gpu}\n";
        echo "Memory: {$this->memory}\n";
        echo "HardDisk: {$this->hardDisk}\n";
    }
}

// 抽象建者类（Builder）
abstract class Builder
{
    protected Computer $computer;
    public function __construct()
    {
        $this->computer = new Computer();
    }
    abstract public function buildCpu(): void;
    abstract public function buildGpu(): void;
    abstract public function buildMemory(): void;
    abstract public function buildHardDisk(): void;
    public function createComputer(): Computer
    {
        return $this->computer;
    }
}

// 具体建造者类（ConcreteBuilder），游戏电脑（GameBuilder）
class GameBuilder extends Builder
{
    public function buildCpu(): void
    {
        $this->computer->setCpu('Intel i7');
    }
    public function buildGpu(): void
    {
        $this->computer->setGpu('NVIDIA GeForce RTX 5980');
    }
    public function buildMemory(): void
    {
        $this->computer->setMemory('ddr5 64G');
    }
    public function buildHardDisk(): void
    {
        $this->computer->setHardDisk('1T SSD');
    }
}
// 具体建造者类（ConcreteBuilder），办公电脑（OfficeBuilder）
class OfficeBuilder extends Builder
{
    public function buildCpu(): void
    {
        $this->computer->setCpu('Intel i7');
    }
    public function buildGpu(): void
    {
        $this->computer->setGpu('Intel UHD Graphics 630');
    }
    public function buildMemory(): void
    {
        $this->computer->setMemory('ddr4 16G');
    }
    public function buildHardDisk(): void
    {
        $this->computer->setHardDisk('1T SSD');
    }
}

// 指挥者类（Director），统一建造者按照一定的步骤来构建产品，屏蔽构建过程中的细节
class Director
{
    public function construct(Builder $builder): Computer
    {
        $builder->buildCpu();
        $builder->buildGpu();
        $builder->buildMemory();
        $builder->buildHardDisk();
        return $builder->createComputer();
    }
}

// 测试
$director = new Director();
// 游戏电脑
$gameBuilder = new GameBuilder();
$gameComputer = $director->construct($gameBuilder);
$gameComputer->show();
// 办公电脑
$officeBuilder = new OfficeBuilder();
$officeComputer = $director->construct($officeBuilder);
$officeComputer->show();